﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Roy.IService;
using Roy.Entity.CommonBO;
using Roy.Entity;
using Roy.Data;

/*********************************************************
 * 开发人员：鲁宁
 * 创建时间：2015/2/2 17:16:49
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Roy.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RoyUserService:IRoyUserService
    {
        /// <summary>
        /// 检查登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>true登录成功,false登录失败</returns>
        public RoyResult<bool> CheckLogin(string username, string password)
        {
            var result = new RoyResult<bool> { ReturnCode = ReturnCodeType.Error };

            Roy.Common.Helper.DBHelper.NoLockInvokeDB(() =>
            {
                using (var db = new RoyDB())
                {
                    var count= db.SM_User.Count(p => p.UserID.Equals(username) && p.Password.Equals(password));
                    if (count > 0)
                    {
                        result.Content = true;
                        result.ReturnCode = ReturnCodeType.Success;
                        result.Message = "登录成功!";
                    }
                    else
                    {
                        result.Content = false;
                        result.Message = "用户名或密码不正确认，请确认后重试!";
                    }
                }
            });

            return result;
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public RoyResult<User> GetAllUsers()
        {
            var result = new RoyResult<User> 
            {
                ReturnCode= ReturnCodeType.Error
            };

            Roy.Common.Helper.DBHelper.NoLockInvokeDB(() =>
            {
                using (var db = new RoyDB())
                {
                    result.Contents= db.SM_User.ToList();
                    result.ReturnCode = ReturnCodeType.Success;
                }
            });

            return result;
        }

    }
}
